package cn.shop.admin.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.shop.admin.entity.backLogModule.BackLog;
import cn.shop.admin.service.BackLogService;
import cn.shop.base.MyPage;
import cn.shop.base.UserInfoForSession;
import cn.shop.utils.ApiResult;
import cn.shop.utils.constant.Constant;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 日志表(BackLog)表控制层
 *
 * @author jzw
 * @since 2021-11-03 10:29:10
 */
@Validated
@RestController
@RequestMapping("/api/v1/backLog")
public class BackLogController {

	/**
	 * 服务对象
	 */
	@Resource
	private BackLogService backLogService;

	/**
	 * 分页查询所有数据
	 *
	 * @param page    分页对象
	 * @param backLog 查询实体
	 * @return 所有数据
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BackLog>> queryByPage(MyPage<BackLog> page, BackLog backLog,
	                                              @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                              @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		SaSession session = StpUtil.getSession(false);
		UserInfoForSession currentUser = (UserInfoForSession) session.get(Constant.USER);
		backLog.setAccount(currentUser.getAccount());
		return ApiResult.success(backLogService.page(page, Wrappers.lambdaQuery(backLog)
				.apply(startTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)));
	}

	/**
	 * 新增数据
	 *
	 * @param backLog 实体对象
	 * @return 新增结果
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@RequestBody BackLog backLog) {
		return ApiResult.result(backLogService.save(backLog));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		BackLog backLog = new BackLog();
		backLog.setId(id);
		return ApiResult.result(backLogService.removeById(backLog));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(backLogService.batchDelete(idList, new BackLog()));
	}

}

